package com.example.androidstudy.activity;

import com.example.androidstudy.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class MenuInflateFromXml extends Activity {
	private static final int sMenuExampleResources[] = { R.menu.title_only,
			R.menu.title_icon, R.menu.submenu, R.menu.shortcuts,
			R.menu.checkable };
	private static final String sMenuExampleNames[] = { "Title Only",
			"Tile And Icon", "SubMenu", "ShortCuts", "Checkable" };
	private Spinner mSpinner;
	private Menu mMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, sMenuExampleNames);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpinner = new Spinner(this);
		mSpinner.setId(1000);
		mSpinner.setAdapter(adapter);
		mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				invalidateOptionsMenu();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
		layout.addView(mSpinner, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		setContentView(layout);
		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		mMenu = menu;
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(sMenuExampleResources[mSpinner.getSelectedItemPosition()], menu);
//		mSpinner.setEnabled(false);
		
		return true;
	}

	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.title_only_first:
			
//			break;

		default:
			if (!item.hasSubMenu()) {
                Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
                return true;
            }
			break;
		}
		return true;
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
